﻿using AutoMapper;
using B.S.Business.Domain;
using B.S.Business.ErrorCode;
using B.S.Business.Infacetrue;
using B.S.Business.Write.API.Applications.Command.SupplierCommand;
using DAL;
using MediatR;

namespace B.S.Business.Write.API.Applications.CommandHandler.SupperlierCommandHandler
{
    public class SupplierDelCommandHandler : IRequestHandler<SupplierDelCommand, ApiResult<object>>
    {
        private readonly IBaseRepository<SupplierApp> supplierRepo;
        private readonly IMapper mapper;
        private readonly DBHelper helper;

        public SupplierDelCommandHandler(IBaseRepository<SupplierApp> supplierRepo, IMapper mapper,DBHelper helper)
        {
            this.supplierRepo = supplierRepo;
            this.mapper = mapper;
            this.helper = helper;
        }
        /// <summary>
        /// 供应商删除处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<object>> Handle(SupplierDelCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();

            //helper.BatchDeleteByIds($"{request.SupplierId}");

            var obj = supplierRepo.GetAll().FirstOrDefault(x => x.SupplierId == request.SupplierId);
            obj.IsDeleted = true;
            var res = supplierRepo.Update(obj);
            if (res == 1)
            {
                result.Code = ApiCode.删除成功;
                result.Msg = ApiCode.删除成功.ToString();
                result.Data = res;
            }
            else
            {
                result.Code = ApiCode.删除失败;
                result.Msg = ApiCode.删除失败.ToString();
                result.Data = res;
            }
            return Task.FromResult(result);
        }
    }
}
